package com.kanche.bigbaby.collection.ui;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.StringUtils;
import com.kanche.bigbaby.collection.ViewUtlis;

//import com.kkche.bss.R;
//import com.kkche.bss.collection.ViewUtlis;
//import com.kkche.bss.utils.StringUtils;

/**
 * Created by nealmi on 6/11/14.
 */
public class ItemEditTextConfig extends ItemConfig {
    private int layoutId = R.layout.input_list_item_edit_text;
    private String hint;
    private String caption;
    private boolean editable;
    private InputFilter[] inputFilters;
    private boolean noMaxWidth;
    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            setValue(editable.toString());
        }
    };

    public ItemEditTextConfig(String key, String value, String label, String hint, String caption) {
        super(key, value, label);
        this.hint = hint;
        this.caption = caption;
    }

    public ItemEditTextConfig(String key, String value, String label, String hint, String caption, boolean editable) {
        super(key, value, label);
        this.hint = hint;
        this.caption = caption;
        this.editable = editable;
    }

    public ItemEditTextConfig() {
    }

    @Override
    public View getView(View convertView, Context mContext) {
        String canonicalName = ItemEditTextConfig.class.getCanonicalName();
//        if (convertView != null)
//            holder = (Holder) convertView.getTag();
//        if (convertView == null || !canonicalName.equals(holder.type)) {
        Holder holder = null;
        holder = new Holder();

        LayoutInflater inflater = LayoutInflater.from(mContext);

        convertView = inflater.inflate(layoutId, null);
        holder.editText = (EditText) convertView.findViewById(R.id.editText);
        holder.editText.setInputType(getInputType());
        holder.caption = (TextView) convertView.findViewById(R.id.caption);
        if (!noMaxWidth) {
            holder.editText.setMaxWidth(ViewUtlis.dip2px(mContext, 300));
            holder.caption.setVisibility(View.VISIBLE);
        }
        holder.label = (TextView) convertView.findViewById(R.id.label);
        holder.type = canonicalName;
        convertView.setTag(holder);
//        }
        holder.label.setText(getLabel());
        holder.editText.setSingleLine(isSingleLine());
        holder.editText.setEnabled(isEditable());
        if (getInputFilters() != null) {
            holder.editText.setFilters(getInputFilters());
        } else {
            holder.editText.setFilters(new InputFilter[]{});
        }
        if (StringUtils.hasText(getValue())) {
            holder.editText.setText(getValue());
        } else {
            holder.editText.setText("");
        }
        if (!isSingleLine()) {
            holder.editText.setLines(4);
        } else {
            holder.editText.setLines(1);
        }
        holder.editText.setHint(getHint());

        if (StringUtils.hasText(getErrorMessage())) {
            holder.editText.setError(getErrorMessage());
        } else {
            holder.editText.setError(null);
        }
        holder.caption.setText(getCaption());
        holder.editText.addTextChangedListener(textWatcher);


        return convertView;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public String getHint() {
        return hint;
    }

    public void setHint(String hint) {
        this.hint = hint;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public InputFilter[] getInputFilters() {
        return inputFilters;
    }

    public void setInputFilters(InputFilter[] inputFilters) {
        this.inputFilters = inputFilters;
    }

    public void setEditTextWidth(boolean noMaxWidth) {
        this.noMaxWidth = noMaxWidth;
    }

    class Holder {
        String type;
        TextView caption, label;
        EditText editText;
    }

//    public static int dip2px(Context context, float dpValue) {
//        final float scale = context.getResources().getDisplayMetrics().density;
//        return (int) (dpValue * scale + 0.5f);
//    }
}
